package cc.ls51.cms.controller;

import cc.ls51.cms.shiro.ShiroUtils;
import cc.ls51.cms.utils.RenderUtil;
import cc.ls51.common.PageUtils;
import cc.ls51.form.UserListForm;
import cc.ls51.modules.SysUser.entity.SysUser;
import cc.ls51.modules.SysUser.service.ISysUserService;
import cc.ls51.vo.ResultObject;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author llf
 * @since 2019-08-27
 */
@Controller
@RequestMapping("/SysUser")
public class SysUserController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    RenderUtil renderUtil;

    @GetMapping(value = "/userList")
    public String userList(UserListForm form, Model model) {
        PageUtils<SysUser> page = sysUserService.getPage(form);
        model.addAttribute("page", page);
        return "/SysUser/userList";
    }

    @RequestMapping(value = "/userEdit")
    public String userEdit(Model model, long id, String v,SysUser objUser) {
        if ("add".equals(v)) {
            return "/SysUser/userEdit";
        } else if ("edit".equals(v)) {
            SysUser user = sysUserService.getById(id);
            if ("admin".equals(user.getUsername())) {
                renderUtil.renderText("管理员不允许编辑");
            }
            model.addAttribute("user", user);
            return "/SysUser/userEdit";
        }else if("save".equals(v)){
            SysUser user = sysUserService.getById(id);
            BeanUtil.copyProperties(objUser,user,CopyOptions.create().setIgnoreNullValue(true));
            sysUserService.saveOrUpdate(user);
            ResultObject ro = ResultObject.ok("保存成功");
            renderUtil.renderJson(ro);
        } else if("resetPwd".equals(v)){
            SysUser user = sysUserService.getById(id);
            if ("admin".equals(user.getUsername())) {
                renderUtil.renderText("管理员不允许编辑");
            }
            model.addAttribute("user", user);
            return "/SysUser/resetPwd";
        } else if("submitResetPwd".equals(v)){
            SysUser user = sysUserService.getById(id);
            BeanUtil.copyProperties(objUser,user,CopyOptions.create().setIgnoreNullValue(true));
            String pwdtmp = ShiroUtils.sha256(objUser.getPassword(),user.getSalt());
            user.setPassword(pwdtmp);
            sysUserService.saveOrUpdate(user);
            ResultObject ro = ResultObject.ok("保存成功");
            renderUtil.renderJson(ro);
        }
        return "/SysUser/userEdit";
    }

    @GetMapping(value = "/index")
    public String index(Model model) {
        SysUser user = ShiroUtils.getUserEntity();
        model.addAttribute("user", user);
        return "/SysUser/index";
    }

    @GetMapping(value = "/logout")
    public String logout() {
        ShiroUtils.logout();
        return "redirect:/login/login";
    }

    @GetMapping(value = "/welcome")
    public String welcome() {
        return "/SysUser/welcome";
    }

    @RequestMapping("modules/{module}/{url}")
    public String module(@PathVariable("module") String module, @PathVariable("url") String url) {
        return "modules/" + module + "/" + url;
    }
}
